/*
 * ServerLauncher.java
 *
 * Created on September 27, 2004, 9:39 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.web;

import java.io.File;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.fpdev.apps.Application;

/**
 *
 * @author demory
 */
public class ServerLauncher extends Application {

  private FPServerImpl server_;
  
  public ServerLauncher() {
    super("server", true);
    
    try {
      server_ = new FPServerImpl(engine_);
    } catch (RemoteException ex) {
      System.out.println("Error initializing FPServerImpl");
      Logger.getLogger(ServerLauncher.class.getName()).log(Level.SEVERE, null, ex);
      return;
    }
    
    System.out.println("Loading network data...");

    String dataSrc = engine_.getDataPackage().getProperty("dataSource");

    if(dataSrc.equals("shp")) {
      engine_.getDataPackage().readNetworkFromSHP();
    }

    if(dataSrc.equals("postgis")) {
      engine_.getDataPackage().readNetworkFromPostgis();
    }

    /*engine_.loadNodesFromDB(true);
    engine_.loadLinksFromDB(true);
    engine_.loadShapePointsFromDB();
    engine_.loadAddrRangesFromDB();
    engine_.loadElevInfoFromDB();
    engine_.loadScenLinkTypesFromDB();*/
    System.gc();
    System.out.println("done loading network");

    System.out.print("Loading route data...");
    engine_.getRoutes().readFromXMLFile(); // readRoutesFromXML(engine_.getProperty("5pHome") + "data" + File.separator + engine_.getDataPackage().getName() + File.separator + "routes.xml");
    engine_.getRoutes().initPaths();
    System.out.println("done");

    System.out.print("Loading station data...");
    engine_.getStations().readFromXML(engine_.getProperty("5pHome") + "data" + File.separator + engine_.getDataPackage().getName() + File.separator + "stations.xml");
    engine_.getStations().initNetworkElements();
    System.out.println("done");

    System.out.print("Building transit network...");
    engine_.buildRouteNetwork();
    System.out.println("done");

    System.out.print("Loading street alias data...");
    engine_.readStreetAliasFile(engine_.getProperty("5pHome") + "data" + File.separator + engine_.getDataPackage().getName() + File.separator + "streetalias.xml");
    System.out.println("done");

    System.out.print("Loading ZIP data...");
    engine_.readZipsFile(engine_.getProperty("5pHome") + "data" + File.separator + engine_.getDataPackage().getName() + File.separator + "zips.xml");
    System.out.println("done");

    System.out.print("Starting server monitor...");
    new ServerMonitor(server_, engine_, 30 * 60000);
    System.out.println("done");
    
    try {
      Naming.rebind("FPServer", server_);
      System.out.println("FPServer is ready.");
    } catch (Exception e) {
      System.out.println("FPServer failed: " + e);
    }
  }
  
  public static void main(String[] argv) {
    new ServerLauncher();
  }
  
}
